package com.shop.cloud.controller.hongfu;

import io.swagger.annotations.ApiOperation;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.dao.hongfu.UserDao;
import com.shop.cloud.domain.hongfu.GoodsOrder;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.domain.hongfu.UserMember;
import com.shop.cloud.domain.hongfu.UserVip;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.model.response.ObjectDataResponse;
import com.shop.cloud.service.hongfu.IGoodsOrderService;
import com.shop.cloud.service.hongfu.IRebateConfigService;
import com.shop.cloud.service.hongfu.IUserService;
import com.shop.cloud.service.hongfu.UserMemberService;
import com.shop.cloud.service.hongfu.UserVipService;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

/**
 * 用户订单管理
 * @author Yan Weiwei
 *
 */
@RestController	
@RequestMapping(value = "api")
public class UserOrderController extends BaseController {

	@Autowired
	private IGoodsOrderService goodsOrderService;
	
	@Autowired
	private IRebateConfigService rebateConfigService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private UserVipService userVipService;
	
	@Autowired
	private UserMemberService userMemberService;
	
	@Autowired
	private UserDao userDao;
	
	@ApiOperation(value = "用户下单", notes = "用户下单")
	@RequestMapping(value = "/placeAnOrder", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> placeAnOrder(@CurrentUserId Long userId, @RequestBody GoodsOrder goodsOrder){
		
		goodsOrder.setUserId(userId);
		int resultData = goodsOrderService.handleplaceAnOrder(goodsOrder);
		
		return new ObjectDataResponse<Integer>(resultData);
	}
	
	@ApiOperation(value = "获取我的订单列表", notes = "获取我的订单列表")
	@RequestMapping(value = "/getMyOrderList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<GoodsOrder>> getMyOrderList(@CurrentUserId Long userId, @RequestBody GoodsOrder goodsOrder){
		
		goodsOrder.setUserId(userId);
		goodsOrder.setOrderBy("GOODS_ORDER.create_time DESC");
		Page<GoodsOrder> data = goodsOrderService.handleOrderList(goodsOrder);
		
		return new ObjectDataResponse<Page<GoodsOrder>>(data);
	}
	
	@ApiOperation(value = "修改我的订单状态", notes = "修改我的订单状态")
	@RequestMapping(value = "/modifyMyOrder", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> modifyMyOrder(@RequestBody GoodsOrder goodsOrder){
		
		int data = goodsOrderService.handleOrderStatus(goodsOrder);
		
		return new ObjectDataResponse<Integer>(data);
	}
	
	@ApiOperation(value = "获取订单详情", notes = "获取订单详情")
	@RequestMapping(value = "/getMyOrderByOne", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<GoodsOrder> getMyOrderByOne(@RequestBody GoodsOrder goodsOrder){
		
		GoodsOrder data = goodsOrderService.handleOrderDetail(goodsOrder);
		
		return new ObjectDataResponse<GoodsOrder>(data);
	}
	
	@ApiOperation(value = "判断用户购买返利商品权限", notes = "判断用户购买返利商品权限")
	@RequestMapping(value = "/getRebateConfig", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<String> getRebateConfig(@CurrentUserId Long userId){
		
		String data = rebateConfigService.handleUserRebate(userId);
		
		return new ObjectDataResponse<String>(data);
	}
	
	@ApiOperation(value = "获取用户详情", notes = "获取用户详情")
	@RequestMapping(value = "/getUserMsgById", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<User> getUserMsgById(@CurrentUserId Long userId){
		
		User user=new User();
		user.setId(userId);
		user = userDao.getGoodRebate(user);
		User data = userService.get(userId);
		//商品返利金额
		data.setGoodRebate(user.getGoodRebate());
		
		return new ObjectDataResponse<User>(data);
	}
	
	@ApiOperation(value = "会员卡设置列表", notes = "会员卡设置列表")
	@RequestMapping(value = "vipList", method = RequestMethod.POST)
	@ResponseBody
	public Response vipList(@RequestBody UserVip userVip){
		userVip.setOrderBy("USER_VIP.integral");
		 Page<UserVip> page= userVipService.page(userVip);
		return ResponseUtil.ok(page);
	}
	
	@ApiOperation(value = "会员卡详情", notes = "会员卡详情")
	@RequestMapping(value = "vipFindOne", method = RequestMethod.POST)
	@ResponseBody
	public Response vipFindOne(@RequestBody UserVip userVip){
	    UserVip page= userVipService.get(userVip.getId());
		return ResponseUtil.ok(page);
	}
	
	@ApiOperation(value = "购买会员卡", notes = "购买会员卡")
	@RequestMapping(value = "userMemberAdd", method = RequestMethod.POST)
	@ResponseBody
	public Response userMemberAdd(@CurrentUserId Long userId,@RequestBody UserVip userVip){
	  	List<UserVip> list=userVipService.find(userVip);
		
	  	if (list.size()>0) {
	  		UserMember userMember=new UserMember();
			userMember.setLevel(list.get(0).getLevel());
			userMember.setUserId(userId);
			userMember.setCreateTime(new Timestamp(new Date().getTime()));
			userMember.setReturnPeriods("0/"+list.get(0).getCount());
			userMember.setPrice(list.get(0).getPrice());
			
			String tradeNo = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ((int)((Math.random()*9+1)*100000));
			userMember.setTradeNo(tradeNo);
			
			int result= userMemberService.addMember(userMember);
			
			return ResponseUtil.ok(result);
		}
	  	return ResponseUtil.ok(0);
	}
	
	@ApiOperation(value = "会员卡购买记录查询", notes = "会员卡购买记录查询")
	@RequestMapping(value = "userMemberList", method = RequestMethod.POST)
	@ResponseBody
	public Response userMemberList(@CurrentUserId Long userId,@RequestBody UserMember userMember){
		 userMember.setUserId(userId);
		 userMember.setCreateTime(new Timestamp(new Date().getTime()));
		 Page<UserMember> page= userMemberService.page(userMember);
		 List<UserMember> memberList=new ArrayList<UserMember>();
		 for (int i = 0; i < page.getData().size(); i++) {
			UserMember um=page.getData().get(i);
			
			um.setLevelRemark("购买"+getNumber(Integer.parseInt(um.getLevel().replaceAll("[a-zA-Z]","" )))+"星会员");
			memberList.add(um);
		}
		 page.setData(memberList);
		return ResponseUtil.ok(page);
	}
	
	
	public static String getNumber(int num){
		if (num==1) {
			return "一";
		}
		if (num==2) {
			return "二";
		}
		if (num==3) {
			return "三";
		}
		if (num==4) {
			return "四";
		}
		if (num==5) {
			return "五";
		}
		
		return null;
	}
	
	
	
}
